package com.cloud.service.impl;

import com.cloud.dao.UserRepository;
import com.cloud.dto.UserDto;
import com.cloud.entity.User;
import com.cloud.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userDao;

    @Override
    public UserDto getUerById(Integer userId) {
        User user = userDao.getOne(userId);
        UserDto dto = new UserDto();
        if (user != null) {
            BeanUtils.copyProperties(user, dto);
        }
        return dto;
    }

    @Override
    public UserDto login(UserDto userDto) {
        User user = userDao.findUserByNameAndPassword(userDto.getName(), userDto.getPassword());
        if (user == null) {
            return null;
        }
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(user, dto);
        return dto;
    }

    @Override
    public List<UserDto> allUsers() {
        List<User> users = userDao.findAll();
        return e2d(users);
    }

    @Override
    public UserDto addUser(UserDto dto) {
        User user = new User();
        BeanUtils.copyProperties(dto, user, new String[]{"id"});
        user.setDel(0);
        user.setIStatus("可邀请");
        user.setTeamId(0);
        userDao.save(user);
        BeanUtils.copyProperties(user, dto);
        return dto;
    }


    @Override
    public List<UserDto> getUserByTeamId(Integer teamId) {
        List<UserDto> userDtoList = new ArrayList<>();
        List<User> userList = userDao.findUserByTeamId(teamId);
        for (int i = 0; i < userList.size(); i++) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userList.get(i), userDto);
            userDtoList.add(userDto);
        }
        return userDtoList;
    }

    /**
     * 接受邀请
     *
     * @param userId
     * @param teamId
     * @return userDto
     */
    @Override
    public UserDto accept(Integer userId, Integer teamId) {
        User user = userDao.getOne(userId);
        user.setTeamId(teamId);
        user.setIStatus("已加入");
        userDao.save(user);
        UserDto dto = e2d(user);

        return dto;
    }

    @Override
    public UserDto update(User user) {
        User user1 = userDao.getOne(user.getUserId());
        user1.setEmail(user.getEmail());
        user1.setPhone(user.getPhone());
        user1.setPassword(user.getPassword());
        user1.setName(user.getName());

        userDao.save(user1);
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(user1, dto);
        return dto;
    }

    @Override
    public List<UserDto> aUser(){
        List<UserDto> dtos =e2d(userDao.aUser(0));
        return dtos;
    }

    private UserDto e2d(User user) {
        if (user == null) {
            return null;
        }
        UserDto userdto = new UserDto();
        BeanUtils.copyProperties(user, userdto);
        return userdto;
    }

    private List<UserDto> e2d(List<User> users) {
        List<UserDto> userdtos = new ArrayList<>();
        if (users != null && users.size() > 0) {
            for (User u : users) {
                userdtos.add(e2d(u));
            }
        }
        return userdtos;
    }

    @Override
    public List<UserDto> findUserByName(String userName){
        List<User> userList=userDao.findUsersByName(userName);
        return e2d(userList);
    }

}
